﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DesktopGarden;
using DesktopGarden.Controls;
using DesktopGarden.Classes;
using DesktopGarden.Windows;
using Microsoft.Win32;
using System.Timers;


    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public static FlowerInstance SelectedFlower { get; set; }

        Brush SELECTEDMODECOLOR = Brushes.Red;

        Brush NONSELECTEDMODECOLOR = Brushes.White;

        Button _selectedModeButton;

        Button SelectedModeButton
        {
            get { return _selectedModeButton; }
            set
            {
                if (SelectedModeButton != null)
                {
                    if(FindResource("NormalBrush") != null)
                    SelectedModeButton.Background = (Brush)FindResource("NormalBrush");
                }
                _selectedModeButton = value;
                SelectedModeButton.Background = SELECTEDMODECOLOR;
            }
                    
        }

        public MainWindow()
        {
            InitializeComponent();
            gardenViewer.ChangeMade = new EventHandler(GardenViewerChangeMade);
            gardenViewer.Saved = new EventHandler(HideSaveControls);
            gardenViewer.Cancelled = new EventHandler(HideSaveControls);

            ChangeMode(App.Mode.Edit, btnEdit);

           // NONSELECTEDMODECOLOR = (Brush)FindResource("NormalBrush");
        }

        private void LoadGarden(Garden garden)
        {
            App.CurrentGarden = garden;
            lblGardenName.Content = App.CurrentGarden.GardenName;
            gardenViewer.LoadGarden(App.CurrentGarden);
            GenerateLegendList();
            btnAddFlower.IsEnabled = true;
            btnEditGarden.IsEnabled = true;         
        }

        private void GenerateLegendList()
        {
            
            //lstLegend.SourceUpdated += (x, y) =>
            //{
                
            //};

            lstLegend.ItemsSource = App.CurrentGarden.Flowers;

            lstLegend.LayoutUpdated += UpdateListToolTips;

        }

        private void UpdateListToolTips(object sender, EventArgs e)
        {

            foreach (FlowerInstance i in lstLegend.Items)
            {
                ListBoxItem lbi = lstLegend.ItemContainerGenerator.ContainerFromItem(i) as ListBoxItem;
                if (lbi != null)
                    lbi.ToolTip = new MiniFlowerDisplay(i);

            }
            lstLegend.LayoutUpdated -= UpdateListToolTips;

        }

        private void LoadToolTips()
        {
        }

        private void ClickCancel(object sender, RoutedEventArgs e)
        {
            gardenViewer.CancelAction();
            gardenViewer.ClearSelection();
        }

        private void SelectionChangeList(object sender, SelectionChangedEventArgs e)
        {
            if (CheckForChanges())
            {
                SelectedFlower = lstLegend.SelectedItem as FlowerInstance;

                gardenViewer.SelectAll(SelectedFlower);
            }
        }

        private void ClickSave(object sender, RoutedEventArgs e)
        {
            gardenViewer.SaveAction();
            gardenViewer.ClearSelection();
        }

        private void ClickDelete(object sender, RoutedEventArgs e)
        {
            ChangeMode(App.Mode.Delete, (sender as Button));

        }

        private void ClickEdit(object sender, RoutedEventArgs e)
        {
            ChangeMode(App.Mode.Edit, (sender as Button));
          
        }

        private void ClickPlant(object sender, RoutedEventArgs e)
        {
            ChangeMode(App.Mode.Plant, (sender as Button));
        }

        private bool CheckForChanges()
        {
            if (gardenViewer.HasChanges)
            {
                MessageBoxResult result = MessageBox.Show("Would you like to save the changes you made?", "Changes", MessageBoxButton.YesNo);
                if (result == MessageBoxResult.Yes)
                    gardenViewer.SaveAction();
                else if (result == MessageBoxResult.No)
                    gardenViewer.CancelAction();
                else
                    return false;

            }

            return true;
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (!CheckForChanges())
                e.Cancel = true;
        }

        private void GardenViewerChangeMade(object sender, EventArgs e)
        {
            ugSaveControls.Visibility = System.Windows.Visibility.Visible;
        }

        private void HideSaveControls(object sender, EventArgs e)
        {
            ugSaveControls.Visibility = System.Windows.Visibility.Collapsed ;
        }

    

        private void ChangeMode(App.Mode mode, Button button)
        {
            if (CheckForChanges())
            {
                App.CurrentMode = mode;
                SelectedModeButton = button;
            }
        }

        private void DoubleClickFlower(object sender, MouseButtonEventArgs e)
        {
            FlowerInstance fl = lstLegend.SelectedItem as FlowerInstance;
            if (fl != null)
            {
                FlowerEditor viewFlower = new FlowerEditor(fl);
               
                if ((bool)viewFlower.ShowDialog())
                {
                    lstLegend.Items.Refresh();
                    //GenerateLegendList();

                        gardenViewer.RefreshFlower(fl);

                    App.CurrentGarden.Save();
                }
                
            }
        }

        private void ClickNewGarden(object sender, RoutedEventArgs e)
        {
                Garden g = new Garden();
                g.GardenName = "New Garden";
                g.Rows = 25;
                g.Columns = 25;
                GardenEditor editor = new GardenEditor(g, "New Garden");
                editor.Owner = this;
                if((bool)editor.ShowDialog())
                {
                    g.Save();
                    LoadGarden(g);
            }   
            

        }

        private void ClickLoadGarden(object sender, RoutedEventArgs e)
        {
            OpenFileDialog file = new OpenFileDialog();
            file.Filter = "Garden Files (*.gdn)|*.gdn";

            if (DesktopGarden.Properties.Settings.Default.LastSaveLocation != null)
                file.InitialDirectory = System.IO.Path.GetDirectoryName(DesktopGarden.Properties.Settings.Default.LastSaveLocation);

             if ((bool)file.ShowDialog())
            {
                Garden g = Garden.Load(file.FileName);
                
                LoadGarden(g);
            }
        }

        private void AddFlower(object sender, RoutedEventArgs e)
        {
            FlowerInstance flower = new FlowerInstance();
            
            FlowerEditor editor = new FlowerEditor(flower);
            if ((bool)editor.ShowDialog())
            {
                App.CurrentGarden.Flowers.Add(flower);
                lstLegend.Items.Refresh();
                App.CurrentGarden.Save();
            }

        }

        private void ClickFlowerCatalog(object sender, RoutedEventArgs e)
        {
            new FlowerCatalog().Show();
        }

        private void ClickDeleteFromGarden(object sender, RoutedEventArgs e)
        {
            FlowerInstance fl = lstLegend.SelectedItem as FlowerInstance;
            if (fl != null)
            {
                if (MessageBox.Show("Are you sure you wish to delete this flower from the garden?", "Are Your Sure", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    App.CurrentGarden.DeleteFlower(fl);
                    gardenViewer.RefreshGarden();
                    lstLegend.Items.Refresh();
                }

            }
        }

        private void EditGarden(object sender, RoutedEventArgs e)
        {

            GardenEditor editor = new GardenEditor(App.CurrentGarden, "Edit Garden");
            editor.Owner = this;
            if ((bool)editor.ShowDialog())
            {
                App.CurrentGarden.Save();
                LoadGarden(App.CurrentGarden);
            }   
        }
    }

